package com.wy.concurrent_sort;

import java.util.Arrays;

/**
 * @author 美女
 * @date 2022/04/21 14:43
 **/
public class ConcurrentSort {
    //进行线程的排序
    static class SortWorker extends Thread{
        private final long[] array;
        private final int fromIndex;
        private final int toIndex;

        //利用构造方法，将待排序数组区间情况传入
        //对array的[fromIndex,toIndex)进行排序
        public SortWorker(long[] array, int fromIndex, int toIndex) {
            this.array = array;
            this.fromIndex = fromIndex;
            this.toIndex = toIndex;
        }

        @Override
        public void run() {
            //具体的排序过程，这里使用Arrays.sort做模拟
            Arrays.sort(array,fromIndex,toIndex);
        }
    }
    //记录排序耗时

    public static void main(String[] args) throws InterruptedException {
        long[] array=ArrayHelper.generateArray(4_000_0000);
        //四个线程排，一个排一千万
        //分别是
        //[0,1000_0000)
        //[10000000,20000000)
        //[20000000,30000000)
        //[30000000,40000000)
        long start=System.currentTimeMillis();
        Thread t1=new SortWorker(array,0,1000_0000);
        t1.start();
        Thread t2=new SortWorker(array,1000_0001,2000_0000);
        t2.start();
        Thread t3=new SortWorker(array,2000_0001,3000_0000);
        t3.start();
        Thread t4=new SortWorker(array,3000_0001,4000_0000);
        t4.start();
        //4个线程开始分别进行排序了
        //等待四个线程全部排序完毕
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        //此时4个线程一定全部结束了
        long end=System.currentTimeMillis();
        long elapsed=end-start;//总共耗时时间
        System.out.println(elapsed);
        //TODO:进行4路归并，将四个有序数组归并成一个有序数组
    }
}
